﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    /// <summary>
    /// Summary description for DALichSuCuocGoi
    /// </summary>
    public class DALichSuCuocGoi : DbObject
    {
        public SqlConnection connec;
        public DALichSuCuocGoi(SqlConnection Connection)
        {
            connec = Connection;
            //
            // TODO: Add constructor logic here
            //
        }

        public DataSet load_lich_su_goi_cua_hoc_vien(int i_id_hoc_vien, int i_vung_level)
        {
            DataSet op_ds_hist_call = new DataSet();
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@HocVienId", SqlDbType.Int,4)
                                             ,new SqlParameter("@VungId", SqlDbType.Int,4)
										 };
            // set the 0values
            parameters[0].Value = i_id_hoc_vien;
            parameters[1].Value = i_vung_level;
            // create the parameter
            op_ds_hist_call = RunProcedure("pr_TB_LichSuCuocGoi_GetLichSuGoi", parameters, "TB_LichSuCuocGoi", connec);
            return op_ds_hist_call;
        }
        public DataSet load_lich_su_goi_all(string ip_str_so_dien_thoai
                                            , DateTime ip_dat_goi_tu_ngay
                                            , DateTime ip_dat_goi_den_ngay)
        {
            DataSet op_ds_hist_call = new DataSet();
            SqlParameter[] parameters =  {	
                                              new SqlParameter("@SoDienThoai", SqlDbType.VarChar,20)
                                             ,new SqlParameter("@GoiTuNgay", SqlDbType.DateTime)
                                             ,new SqlParameter("@GoiDenNgay", SqlDbType.DateTime)
										 };
            // set the 0values
            parameters[0].Value = ip_str_so_dien_thoai;
            parameters[1].Value = ip_dat_goi_tu_ngay;
            parameters[2].Value = ip_dat_goi_den_ngay;
            // create the parameter
            op_ds_hist_call = RunProcedure("pr_TB_LichSuCuocGoi_GetSearchLichSuGoi", parameters, "TB_LichSuCuocGoi", connec);
            return op_ds_hist_call;
        }
        public void load_call_infor_by_id(int ip_id)
        {
            DataSet v_ds_call_infor = new DataSet();
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@Id", SqlDbType.Int,4)
										 };
            // set the 0values
            parameters[0].Value = ip_id;
            // create the parameter
            v_ds_call_infor = RunProcedure("pr_TB_LichSuCuocGoi_GetCallInfor_ById", parameters, "TB_LichSuCuocGoi", connec);
            this.data = new CallInfor();
            if (v_ds_call_infor.Tables[0].Rows.Count > 0)
            {
                data.link_down_record = v_ds_call_infor.Tables[0].Rows[0]["LinkDownRecord"].ToString();
            }
            else data.link_down_record = "NOTHING";
        }

        public DataSet get_hiscall_cm(int i_id_hoc_vien)
        {
            DataSet op_hiscall_cm = new DataSet();
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@HocVienId", SqlDbType.Int,4)                             
                                            
										 };
            // set the 0values
            parameters[0].Value = i_id_hoc_vien;

            // create the parameter
            op_hiscall_cm = RunProcedure("pr_TB_LichSuCuocGoi_cm_GetHisCall", parameters, "TB_LichSuCuocGoi", connec);
            return op_hiscall_cm;
        }
        public DataSet get_last_call(int i_id_hoc_vien, int i_id_vungId)
        {
            DataSet op_ds_lastcall = new DataSet();
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@HocVienId", SqlDbType.Int,4) ,                              
                                             new SqlParameter("@VungId", SqlDbType.Int,4)
										 };
            // set the 0values
            parameters[0].Value = i_id_hoc_vien;
            parameters[1].Value = i_id_vungId;
            // create the parameter
            op_ds_lastcall = RunProcedure("pr_TB_LichSuCuocGoi_GetLastCall", parameters, "TB_LichSuCuocGoi", connec);
            return op_ds_lastcall;
        }
        public DateTime string_2_datetime(string ip_str_datetime)
        {
            string v_str_year = ip_str_datetime.Substring(0, 4);
            string v_str_month = ip_str_datetime.Substring(4, 2);
            string v_str_day = ip_str_datetime.Substring(6, 2);
            string v_str_hour = ip_str_datetime.Substring(8, 2);
            string v_str_minute = ip_str_datetime.Substring(10, 2);
            string v_str_second = ip_str_datetime.Substring(12, 2);
            DateTime dat_out_datetime = new DateTime(Convert.ToInt32(v_str_year)
                                                , Convert.ToInt32(v_str_month)
                                                , Convert.ToInt32(v_str_day)
                                                , Convert.ToInt32(v_str_hour)
                                                , Convert.ToInt32(v_str_minute)
                                                , Convert.ToInt32(v_str_second));
            return dat_out_datetime;
        }
        public int update_lich_su_goi(int intTrangThaiId, int intLevelID)
        {
            int intResult = 0;
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@HocVienId", SqlDbType.Int,4)
                                             ,new SqlParameter("@LevelId", SqlDbType.Int,4)
                                             ,new SqlParameter("@CallId", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@AgentCode", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@MobilePhone", SqlDbType.VarChar,20)
                                             ,new SqlParameter("@StartTime", SqlDbType.DateTime)
                                             ,new SqlParameter("@EndTime", SqlDbType.DateTime)
                                             ,new SqlParameter("@datetime_respond", SqlDbType.DateTime)
                                             ,new SqlParameter("@StationId", SqlDbType.Int,4)
                                             ,new SqlParameter("@LinkDownRecord", SqlDbType.NVarChar,1000)
                                             ,new SqlParameter("@Duration", SqlDbType.Decimal)
                                             ,new SqlParameter("@NoiDungCuocGoi", SqlDbType.NVarChar,1000)
                                             ,new SqlParameter("@error_code", SqlDbType.Int,2)
                                             ,new SqlParameter("@error_desc", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@status", SqlDbType.Int,2)
                                             ,new SqlParameter("@ringtime", SqlDbType.Decimal)
                                            
										 };

            DateTime datEndTime, datStartTime, datRespond_time;
            if (data.end_time != null)
                datEndTime = string_2_datetime(data.end_time);
            else datEndTime = Convert.ToDateTime("01/01/1900");
            if (data.start_time != null)
                datStartTime = string_2_datetime(data.start_time);
            else datStartTime = Convert.ToDateTime("01/01/1900");
            datRespond_time = string_2_datetime(data.datetime_response);

            // set the values
            parameters[0].Value = IntHocVienId;
            parameters[1].Value = intLevelID;
            parameters[2].Value = data.call_id;
            parameters[3].Value = data.agent_code;
            parameters[4].Value = data.mobile_phone;
            parameters[5].Value = datStartTime;
            parameters[6].Value = datEndTime;
            parameters[7].Value = datRespond_time;
            parameters[8].Value = data.station_id;
            parameters[9].Value = data.link_down_record;
            parameters[10].Value = data.duration;
            parameters[11].Value = StrNoiDungCuocGoi;
            parameters[12].Value = data.error_code;
            parameters[13].Value = data.error_desc;
            parameters[14].Value = data.status;
            parameters[15].Value = data.ringtime;
           
            try
            {
                RunProcedure("sp_TB_LichSuGoi_update_lich_su_goi", parameters, out intResult, connec);
            }
            catch (Exception v_e)
            {
                //Utilities.WriteToLog(v_e);
            }
            return 1;
        }
        public int update_lich_su_goi_cm(int intTrangThaiId)
        {
            int intResult = 0;
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@HocVienId", SqlDbType.Int,4)
                                         
                                             ,new SqlParameter("@CallId", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@AgentCode", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@MobilePhone", SqlDbType.VarChar,20)
                                             ,new SqlParameter("@StartTime", SqlDbType.DateTime)
                                             ,new SqlParameter("@EndTime", SqlDbType.DateTime)
                                             ,new SqlParameter("@StationId", SqlDbType.Int,4)
                                             ,new SqlParameter("@LinkDownRecord", SqlDbType.NVarChar,1000)
                                             ,new SqlParameter("@Duration", SqlDbType.Decimal)
                                             ,new SqlParameter("@NoiDungCuocGoi", SqlDbType.NVarChar,1000)
                                             ,new SqlParameter("@error_code", SqlDbType.Int,2)
                                             ,new SqlParameter("@error_desc", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@TrangThai", SqlDbType.Int,2)
                                             ,new SqlParameter("@ringtime", SqlDbType.Decimal)
                                            
										 };

            DateTime datEndTime, datStartTime;
            if (data.end_time != null)
                datEndTime = string_2_datetime(data.end_time);
            else datEndTime = Convert.ToDateTime("01/01/1900");
            if (data.start_time != null)
                datStartTime = string_2_datetime(data.start_time);
            else datStartTime = Convert.ToDateTime("01/01/1900");


            // set the values
            parameters[0].Value = IntHocVienId;

            parameters[1].Value = data.call_id;
            parameters[2].Value = data.agent_code;
            parameters[3].Value = data.mobile_phone;
            parameters[4].Value = datStartTime;
            parameters[5].Value = datEndTime;
            parameters[6].Value = data.station_id;
            parameters[7].Value = data.link_down_record;
            parameters[8].Value = data.duration;
            parameters[9].Value = StrNoiDungCuocGoi;
            parameters[10].Value = data.error_code;
            parameters[11].Value = data.error_desc;
            parameters[12].Value = intTrangThaiId;
            parameters[13].Value = data.ringtime;

            try
            {
                RunProcedure("sp_TB_LichSuGoi_update_lich_su_goi_cm", parameters, out intResult, connec);
            }
            catch (Exception v_e)
            {
                //Utilities.WriteToLog(v_e);
            }
            return 1;
        }
        public int insert_lich_su_goi()
        {
            int intResult = 0;
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@HocVienId", SqlDbType.Int,4)
                                             ,new SqlParameter("@CallId", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@MobilePhone", SqlDbType.VarChar,20)
                                             ,new SqlParameter("@StationId", SqlDbType.Int,4)
                                             ,new SqlParameter("@error_code", SqlDbType.Int,2)
                                             ,new SqlParameter("@error_desc", SqlDbType.VarChar,50)
										 };

            // set the values
            parameters[0].Value = IntHocVienId;
            parameters[1].Value = data.call_id;
            parameters[2].Value = data.mobile_phone;
            parameters[3].Value = data.station_id;
            parameters[4].Value = data.error_code;
            parameters[5].Value = data.error_desc;
            try
            {
                RunProcedure("sp_TB_LichSuGoi_insert_call_initial_time", parameters, out intResult, connec);
            }
            catch (Exception v_e)
            {
                //Utilities.WriteToLog(v_e);
            }
            return 1;
        }
        public int insert_lich_su_goi_cm()
        {
            int intResult = 0;
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@HocVienId", SqlDbType.Int,4)
                                             ,new SqlParameter("@CallId", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@MobilePhone", SqlDbType.VarChar,20)
                                             ,new SqlParameter("@StationId", SqlDbType.Int,4)
                                             ,new SqlParameter("@error_code", SqlDbType.Int,2)
                                             ,new SqlParameter("@error_desc", SqlDbType.VarChar,50)
										 };

            // set the values
            parameters[0].Value = IntHocVienId;
            parameters[1].Value = data.call_id;
            parameters[2].Value = data.mobile_phone;
            parameters[3].Value = data.station_id;
            parameters[4].Value = data.error_code;
            parameters[5].Value = data.error_desc;
            try
            {
                RunProcedure("sp_TB_LichSuGoi_insert_call_initial_time_cm", parameters, out intResult, connec);
            }
            catch (Exception v_e)
            {

            }
            return 1;
        }
        public int insert_lich_su_goi_ko_co_callid(string strSoDienThoai, string ip_stationId, int ip_levelId)
        {
            int intResult = 0;
            SqlParameter[] parameters =  {	
                                             new SqlParameter("@HocVienId", SqlDbType.Int,4)
                                             ,new SqlParameter("@NoiDungCuocGoi", SqlDbType.NVarChar,1000)
                                             ,new SqlParameter("@MobilePhone", SqlDbType.VarChar,20)
                                             ,new SqlParameter("@StationId", SqlDbType.VarChar,8)
                                             ,new SqlParameter("@LevelId", SqlDbType.Int,4)
                                             ,new SqlParameter("@TrangThaiId", SqlDbType.Int,4)
										 };

            // set the values
            parameters[0].Value = IntHocVienId;
            parameters[1].Value = StrNoiDungCuocGoi;
            parameters[2].Value = strSoDienThoai;
            parameters[3].Value = ip_stationId;
            parameters[4].Value = ip_levelId;
            parameters[5].Value = IntLevelId;
            try
            {
                RunProcedure("sp_TB_LichSuGoi_insert_call_ko_callId_test", parameters, out intResult, connec);
            }
            catch (Exception v_e)
            {
                //Utilities.WriteToLog(v_e);
            }
            return 1;
        }

        public int insert_miss_history_call(int intLevelId)
        {
            int intResult = 0;
            SqlParameter[] parameters =  {	
                                              new SqlParameter("@LevelId", SqlDbType.Int,4)
                                             ,new SqlParameter("@CallId", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@AgentCode", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@MobilePhone", SqlDbType.VarChar,20)
                                             ,new SqlParameter("@StartTime", SqlDbType.DateTime)
                                             ,new SqlParameter("@EndTime", SqlDbType.DateTime)
                                             ,new SqlParameter("@datetime_respond", SqlDbType.DateTime)
                                             ,new SqlParameter("@StationId", SqlDbType.Int,4)
                                             ,new SqlParameter("@LinkDownRecord", SqlDbType.NVarChar,1000)
                                             ,new SqlParameter("@Duration", SqlDbType.Decimal)
                                             ,new SqlParameter("@NoiDungCuocGoi", SqlDbType.NVarChar,1000)
                                             ,new SqlParameter("@error_code", SqlDbType.Int,2)
                                             ,new SqlParameter("@error_desc", SqlDbType.VarChar,50)
                                             ,new SqlParameter("@status", SqlDbType.Int,2)
                                             ,new SqlParameter("@ringtime", SqlDbType.Decimal)
										 };

            DateTime datEndTime, datStartTime, datRespond_time;
            if (data.end_time != null)
                datEndTime = string_2_datetime(data.end_time);
            else datEndTime = Convert.ToDateTime("01/01/1900");
            if (data.start_time != null)
                datStartTime = string_2_datetime(data.start_time);
            else datStartTime = Convert.ToDateTime("01/01/1900");
            datRespond_time = string_2_datetime(data.datetime_response);

            StrNoiDungCuocGoi = "";

            // set the values
            parameters[0].Value = intLevelId;
            parameters[1].Value = data.call_id;
            parameters[2].Value = data.agent_code;
            parameters[3].Value = data.mobile_phone;
            parameters[4].Value = datStartTime;
            parameters[5].Value = datEndTime;
            parameters[6].Value = datRespond_time;
            if (data.station_id != "")
                parameters[7].Value = Convert.ToDecimal(data.station_id);
            else parameters[7].Value = 0;
            parameters[8].Value = data.link_down_record;
            parameters[9].Value = Convert.ToDecimal(data.duration);
            parameters[10].Value = StrNoiDungCuocGoi;
            parameters[11].Value = Convert.ToInt32(data.error_code);
            parameters[12].Value = data.error_desc;
            parameters[13].Value = Convert.ToInt32(data.status);
            parameters[14].Value = Convert.ToDecimal(data.ringtime);
            try
            {
                RunProcedure("sp_TB_LichSuGoi_insert_miss_history_call", parameters, out intResult, connec);
            }
            catch (Exception v_e)
            {
                //Utilities.WriteToLog(v_e);
            }
            return 1;
        }
        public int IntHocVienId;
        public int IntLevelId;
        public string StrNoiDungCuocGoi;

        public CallInfor data { get; set; }
    }

    public class CallInfor
    {
        public string message_code { get; set; }
        public string agent_code { get; set; }
        public string station_id { get; set; }
        public string mobile_phone { get; set; }
        public string datetime_response { get; set; }
        public string call_id { get; set; }
        public string start_time { get; set; }
        public string end_time { get; set; }
        public string duration { get; set; }
        public string ringtime { get; set; }
        public string link_down_record { get; set; }
        public string status { get; set; }
        public string error_code { get; set; }
        public string error_desc { get; set; }
    }
}